How to Exclude Files and Directories with Rsync

Rsync is a fast and versatile command line utility that synchronizes files and folders between two locations over a remote shell.

With Rsync you can mirror data, create incremental backups and copy files between systems. When copying data, you may want to exclude one or more files or directories based on their name or location.

In this tutorial, we will show you how to exclude files and directories with rsync.

Before You Begin

You should have a basic knowledge of how rsync works .

In the examples below, we will use rsync with the -a, option. This tells rsync to syncs directories recursively, transfer special and block devices and preserve symbolic links, modification times, group, ownership, and permissions.

When excluding files or directories you need to use their relative paths to the source directory.

There are two options to specify the files and directories you want to exclude:

Exclude a Specific File

To exclude a specific file, pass the relative path to the file to the --exclude option.

In the following example the file src_directory/file.txt will not be transferred:

 

rsync -a --exclude 'file.txt' src_directory/ dst_directory/

 

Exclude a Specific Directory

Excluding a specific directory is same as excluding a file, just pass the relative path to the directory to the --exclude option as shown below:

rsync -a --exclude 'dir1' src_directory/ dst_directory/

 

If you want to exclude the directory content but not the directory itself use dir1/* instead of dir1:

rsync -a --exclude 'dir1/*' src_directory/ dst_directory/

 

Exclude Multiple Files or Directories

To exclude multiple files or directories simply specify multiple --exclude options:

rsync -a --exclude 'file1.txt' --exclude 'dir1/*' --exclude 'dir2' src_directory/ dst_directory/

 

If you prefer to use a single --exclude option you can list the files and directories you want to exclude in curly braces {} separated by a comma as shown below:

rsync -a --exclude={'file1.txt','dir1/*','dir2'} src_directory/ dst_directory/

 

If the number of the files and/or directories you want to exclude is large, instead of using multiple --exclude options you can specify the files and directories you want to exclude in a file and pass the file to the --exclude-from option.

The command below does exactly the same as the one above:

rsync -a --exclude-from='exclude-file.txt' src_directory/ dst_directory/

 

exclude-file.txt

 

file1.txt

dir1/*

dir2

Exclude Multiple Files or Directories Based on a Pattern

With rsync you can also exclude files and directories based on a pattern that matches the file or directory name.

For example, to exclude all .jpg files you would run:

rsync -a --exclude '*.jpg*' src_directory/ dst_directory/

 

It is little trickier to exclude all other files and directories except those that match a certain pattern. Let’s say you want to exclude all other files and directories except the files ending with .jpg.

One option is to use the following command:

rsync -a -m --include='*.jpg' --include='*/' --exclude='*' src_directory/ dst_directory/

 

When using multiple include/exclude option, the first matching rule applies.

 

Another option would be to pipe the output of the find command to rsync:

find src_directory/ -name "*.jpg" -printf %P\\0\\n | rsync -a --files-from=- src_directory/ dst_directory/

 

Conclusion

In this tutorial, you learned how to exclude files and directories when transferring data with Rsync. There’s lots more to learn about Rsync at Rsync User’s Manual page.